<template>
  <el-container class="home-container">
    <el-header class="header">
      <div class="header-content">
        <h1>Todo List</h1>
        <el-button
          type="danger"
          @click="handleLogout"
          class="logout-button"
        >
          退出登录
        </el-button>
      </div>
    </el-header>

    <el-main>
      <el-card class="todo-card">
        <div class="todo-actions">
          <el-button
            type="primary"
            @click="showAddDialog = true"
          >
            添加Todo
          </el-button>
        </div>

        <el-table
          :data="todoStore.todos"
          v-loading="todoStore.loading"
          empty-text="暂无数据"
        >
          <el-table-column
            prop="title"
            label="标题"
            width="200"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column
            label="状态"
            width="160"
          >
            <template #default="{ row }">
              <el-select
                v-model="row.completed"
                @change="handleStatusChange(row)"
                size="small"
              >
                <el-option
                  :value="true"
                  label="已完成"
                />
                <el-option
                  :value="false"
                  label="未完成"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="180"
          >
            <template #default="{ row }">
              <el-button
                type="text"
                @click="handleEdit(row)"
              >
                编辑
              </el-button>
              <el-button
                type="text"
                @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-main>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      :title="isEdit ? '编辑Todo' : '添加Todo'"
      v-model="showAddDialog"
      width="30%"
    >
      <el-form
        :model="currentTodo"
        :rules="todoRules"
        ref="todoForm"
      >
        <el-form-item
          label="标题"
          prop="title"
        >
          <el-input v-model="currentTodo.title" />
        </el-form-item>
        <el-form-item
          label="描述"
          prop="description"
        >
          <el-input
            v-model="currentTodo.description"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button
          type="primary"
          @click="handleSubmit"
        >
          确认
        </el-button>
      </template>
    </el-dialog>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useAuthStore } from '../stores/auth'
import { useTodoStore } from '../stores/todo'

const authStore = useAuthStore()
const todoStore = useTodoStore()

const showAddDialog = ref(false)
const isEdit = ref(false)
const currentTodo = ref({
  id: null,
  title: '',
  description: '',
  completed: false
})

const todoRules = {
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入描述', trigger: 'blur' }
  ]
}

onMounted(async () => {
  await todoStore.fetchTodos()
})

const handleLogout = () => {
  authStore.logout()
}

const handleEdit = (todo) => {
  currentTodo.value = { ...todo }
  isEdit.value = true
  showAddDialog.value = true
}

const handleDelete = async (id) => {
  try {
    await todoStore.deleteTodo(id)
  } catch (error) {
    console.error(error)
  }
}

const handleStatusChange = async (todo) => {
  try {
    await todoStore.updateTodo(todo.id, {
      ...todo,
      completed: todo.completed
    })
  } catch (error) {
    console.error(error)
  }
}

const handleSubmit = async () => {
  try {
    if (isEdit.value) {
      await todoStore.updateTodo(currentTodo.value.id, {
        title: currentTodo.value.title,
        description: currentTodo.value.description
      })
    } else {
      await todoStore.addTodo({
        title: currentTodo.value.title,
        description: currentTodo.value.description,
        completed: false
      })
    }
    showAddDialog.value = false
    resetForm()
  } catch (error) {
    console.error(error)
  }
}

const resetForm = () => {
  currentTodo.value = {
    id: null,
    title: '',
    description: '',
    completed: false
  }
  isEdit.value = false
}
</script>

<style scoped>
.home-container {
  height: 100vh;
  background: #f5f7fa;
}

.header {
  background: linear-gradient(135deg, #409EFF 0%, #337ecc 100%);
  color: white;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.header-content {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.todo-card {
  margin: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.todo-actions {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}

.logout-button {
  margin-left: auto;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
}

.el-table::before {
  display: none;
}

.el-table th {
  background-color: #f5f7fa;
}

.el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

.el-dialog {
  border-radius: 8px;
}

.el-form-item__label {
  font-weight: 500;
}

.el-button--primary {
  background: linear-gradient(135deg, #409EFF 0%, #337ecc 100%);
  border: none;
}

.el-button--primary:hover {
  opacity: 0.9;
}

.el-tag {
  border-radius: 12px;
  padding: 0 12px;
}
</style>
